package xyz.jcat.seata.example.bussiness.service.impl;

import io.seata.spring.annotation.GlobalTransactional;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;
import xyz.jcat.seata.example.api.IOrderService;
import xyz.jcat.seata.example.api.IPointsService;
import xyz.jcat.seata.example.api.IStorageService;
import xyz.jcat.seata.example.api.dto.PointsIncrDto;
import xyz.jcat.seata.example.api.dto.SaleDto;
import xyz.jcat.seata.example.api.dto.StorageDecrDto;
import xyz.jcat.seata.example.bussiness.service.IBussinessService;

@Service
public class BussinessService implements IBussinessService {

    @DubboReference
    private IOrderService orderService;
    @DubboReference
    private IPointsService pointsService;
    @DubboReference
    private IStorageService storageService;

    @Override
    @GlobalTransactional(
            rollbackFor = Exception.class
    )
    public void sale(SaleDto saleDto) {
        //创建订单
        orderService.addOrder(saleDto);
        //增加积分
        PointsIncrDto pointsIncrDto = new PointsIncrDto();
        pointsIncrDto.setUserId(saleDto.getUserId());
        pointsIncrDto.setPoints(saleDto.getMoney()/10);
        pointsService.incr(pointsIncrDto);
        //扣减库存
        StorageDecrDto storageDecrDto = new StorageDecrDto();
        storageDecrDto.setItemId(saleDto.getItemId());
        storageDecrDto.setNum(saleDto.getNum());
        storageService.decr(storageDecrDto);
    }

}
